package wiinf;

import javax.swing.JComboBox;

/**
 *
 * @author Randy van Heijst
 */
public class oplBewerken extends javax.swing.JPanel {
    private serverViewModel model;

    /**
     * Aanmaken nieuwe panel voor opleidingen bewerken
     *
     * @param model De JFrame waar de panel in komt
     */
    public oplBewerken(serverViewModel model) {
        this.model = model;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CoplAfdeling = new javax.swing.JComboBox();
        CoplOpleiding = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        ToplNaam = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        CoplAfdelingNieuw = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ToplInfo = new javax.swing.JTextArea();
        ToplLocatie = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        BoplOpslaan = new javax.swing.JButton();

        jLabel1.setText("Afdeling:");

        CoplAfdeling.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        model.setAfdelingBox(CoplAfdeling);
        CoplAfdeling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CoplAfdelingActionPerformed(evt);
            }
        });

        CoplOpleiding.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        CoplOpleiding.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                CoplOpleidingItemStateChanged(evt);
            }
        });

        jLabel2.setText("Opleiding:");

        jLabel3.setText("Naam opleiding:");

        jLabel4.setText("Nieuwe Afdeling:");

        CoplAfdelingNieuw.setModel(new javax.swing.DefaultComboBoxModel(new String[] {} ));

        jLabel5.setText("Locatie opleiding:");

        jLabel6.setText(" Informatie opleiding:");

        ToplInfo.setColumns(20);
        ToplInfo.setRows(5);
        jScrollPane1.setViewportView(ToplInfo);

        BoplOpslaan.setText("Opslaan");
        BoplOpslaan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BoplOpslaanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(146, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(CoplOpleiding, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CoplAfdeling, 0, 168, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(ToplLocatie, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(CoplAfdelingNieuw, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ToplNaam, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(319, Short.MAX_VALUE)
                        .addComponent(BoplOpslaan)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CoplAfdeling, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CoplOpleiding, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ToplNaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CoplAfdelingNieuw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ToplLocatie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BoplOpslaan)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Itemlistener horende bij de cobo box gevuld met opleidingen
     *
     * @param evt Item event
     */
    private void CoplOpleidingItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_CoplOpleidingItemStateChanged
        if(evt.getStateChange()==2){
            String afdeling = CoplAfdeling.getSelectedItem().toString();
            String opleiding = CoplOpleiding.getSelectedItem().toString();
            if(!opleiding.isEmpty()){
                String[] data = model.server.fh.getXMLOpleiding(afdeling, opleiding);

                ToplNaam.setText(data[0]);
                ToplLocatie.setText(data[1]);
                ToplInfo.setText(data[2]);
            }
            model.setAfdelingBox(CoplAfdelingNieuw);
            CoplAfdelingNieuw.setSelectedIndex(CoplAfdeling.getSelectedIndex());
        }
    }//GEN-LAST:event_CoplOpleidingItemStateChanged

    /**
     * Actionlistener horende bij de opslaan button
     *
     * @param evt Action event
     */
    private void BoplOpslaanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BoplOpslaanActionPerformed
        String naamOud = CoplOpleiding.getSelectedItem().toString();
        String naamNieuw = ToplNaam.getText();
        String afdelingOud = CoplAfdeling.getSelectedItem().toString();
        String afdelingNieuw = CoplAfdelingNieuw.getSelectedItem().toString();
        String locatie = ToplLocatie.getText();
        String info = ToplInfo.getText();

        String[] data={naamNieuw,locatie,info};
        model.server.updateOpleiding(naamOud, naamNieuw, afdelingOud, afdelingNieuw, data);
    }//GEN-LAST:event_BoplOpslaanActionPerformed

    /**
     * Actionlistener voor combobox gevuld met afdelingen
     *
     * @param evt Action event
     */
    private void CoplAfdelingActionPerformed(java.awt.event.ActionEvent evt) {
        JComboBox cb = (JComboBox) evt.getSource();
        String afdeling = (String) cb.getSelectedItem();

        String[] opleiding = model.server.fh.getOpleiding(afdeling);
        CoplOpleiding.removeAllItems();
        CoplOpleiding.addItem("");
        if (opleiding.length > 0) {
            for (int i = 0; i < opleiding.length; i++) {
                CoplOpleiding.addItem(opleiding[i].replace(".xml",""));
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BoplOpslaan;
    private javax.swing.JComboBox CoplAfdeling;
    private javax.swing.JComboBox CoplAfdelingNieuw;
    private javax.swing.JComboBox CoplOpleiding;
    private javax.swing.JTextArea ToplInfo;
    private javax.swing.JTextField ToplLocatie;
    private javax.swing.JTextField ToplNaam;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
